import Vue from 'vue' // 如果已引用，不需要重复引用
import Router from 'vue-router' // 如果已引用，不需要重复引用
import { getToken } from '@/request/token'
import store from '@/store'
import { Message } from 'element-ui'
Vue.use(Router) // 如果已引用，不需要重复引用

// push
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

// replace
const VueRouterReplace = Router.prototype.replace
Router.prototype.replace = function replace (to) {
  return VueRouterReplace.call(this, to).catch(err => err)
}

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'root',
      hideInMenu: true,
      redirect: '/blog/home'
    },
    {
      path: '/blog',
      name: 'blog',
      hideInMenu: true,
      component: () =>
        import(/* webpackChunkName:"layouts" */ '../layouts/BasicLayout'),
      children: [
        {
          path: 'home',
          name: 'home',
          component: () => import(/* webpackChunkName:"home" */ '../views/Index')
        }
      ]
    },
    {
      path: '/article',
      name: 'article',
      component: () => import(/* webpackChunkName:"layouts" */ '../layouts/BasicLayout'),
      children: [
        {
          path: 'byId/:id',
          name: 'articleById',
          component: () => import(/* webpackChunkName:"article" */ '../views/BlogView')
        }
      ]
    },
    {
      path: '/category',
      name: 'category',
      component: () =>
        import(/* webpackChunkName:"layouts" */ '../layouts/BasicLayout'),
      children: [
        {
          path: 'categorys',
          name: 'categoryAll',
          component: () => import(/* webpackChunkName:"category" */ '../views/Category')
        },
        {
          path: 'byId/:id',
          name: 'articleByCategory',
          component: () => import(/* webpackChunkName:"category" */ '../views/BlogCategoryTag')
        }
      ]
    },
    {
      path: '/tag',
      name: 'tag',
      component: () =>
        import(/* webpackChunkName:"layouts" */ '../layouts/BasicLayout'),
      children: [
        {
          path: 'tags',
          name: 'tagAll',
          component: () => import(/* webpackChunkName:"tag" */ '../views/Tag')
        },
        {
          path: 'byId/:id',
          name: 'articleByTag',
          component: () => import(/* webpackChunkName:"tag" */ '../views/BlogCategoryTag')
        }
      ]
    },
    {
      path: '/archive',
      name: 'archive',
      component: () =>
        import(/* webpackChunkName:"layouts" */ '../layouts/BasicLayout'),
      children: [
        {
          path: 'archives',
          name: 'archiveAll',
          component: () => import(/* webpackChunkName:"archive" */ '../views/archive/Archive')
        },
        {
          path: 'byDate/:year/:month',
          name: 'articleByArchive',
          component: () => import(/* webpackChunkName:"archive" */ '../views/archive/Archive')
        }
      ]
    },
    {
      path: '/feedback',
      name: 'feedback',
      component: () =>
        import(/* webpackChunkName:"layouts" */ '../layouts/BasicLayout'),
      children: [
        {
          path: 'feedbacks',
          name: 'feedbackIndex',
          component: () => import(/* webpackChunkName:"feedback" */ '../views/Feedback')
        },
        {
          path: 'byId/:feedbackId',
          name: 'articleByFeedback',
          component: () => import(/* webpackChunkName:"feedback" */ '../views/BlogView')
        }
      ]
    },
    {
      path: '/write',
      name: 'blogWrite',
      meta: {
        requireLogin: true
      },
      component: () =>
        import(/* webpackChunkName:"blogWrite" */ '@/views/blogwrite/BlogWrite')
    },
    {
      path: '/edit/byId/:id',
      name: 'blogEdit',
      meta: {
        requireLogin: true
      },
      component: () =>
        import(/* webpackChunkName:"blogWrite" */ '@/views/blogwrite/BlogWrite')
    },
    {
      path: '/login',
      name: 'login',
      component: () =>
        import(/* webpackChunkName:"blogWrite" */ '@/views/Login')
    },
    {
      path: '/register',
      name: 'register',
      component: () =>
        import(/* webpackChunkName:"blogWrite" */ '@/views/Register')
    },
    {
      path: '/back/login',
      name: 'backLogin',
      component: () => import(/* webpackChunkName:"backLogin" */ '@/backview/Login')
    },
    {
      path: '/back',
      name: 'back',
      component: () => import(/* webpackChunkName:"back" */ '@/backview/layouts/BasicLayout'),
      children: [
        {
          path: 'index',
          name: 'index',
          component: () => import(/* webpackChunkName:"back" */ '@/backview/views/index')
        }
      ]
    }
  ],

  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})

router.beforeEach((to, from, next) => {
  if (getToken()) { // 有token
    if (to.path === '/login' || to.path === '/register') {
      next({ path: '/' })
    } else {
      if (store.state.account.length === 0) {
        store.dispatch('getUserInfo').then(data => { // 获取用户信息
          next()
        })
          .catch(() => {
            next({ path: '/' })
          })
      } else {
        next()
      }
    }
  } else { // 无token
    if (to.matched.some(r => r.meta.requireLogin)) {
      Message({
        type: 'warning',
        showClose: true,
        message: '请先登录哦'
      })
      next({ path: '/' })
    } else {
      next()
    }
  }
})

export default router
